﻿$(document).ready(function () {

    searchEnterprise();


    $('#btnSearchEnterpriseReload').click(function () {
        searchEnterprise();

    });


    $('#btnSearchEnterpriseAdd').click(function () {
        window.location.href = "AddEnterprise";
    });


});

/*Función para la utilización de controles dentro de la tabla generada*/
function repaintSearchEnterprise() {

    $('#tblSearchEnterprise tr .colSearchEnterpriseEdit a').click(function () {

    });

    $('#tblSearchEnterprise tr .colSearchEnterpriseView a').click(function () {
     

    });
}

/*Función la busqueda de empresas*/
function searchEnterprise() {

        $.ajax({
            type: "POST",
            data: JSON.stringify(),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            url: "getEnterpriseList",
            success: function (data) {
                if (data == null) {
                    //procedimiento de error
                }
                else {

                    var result="";

                    $.each(data, function (i, item) {

                        var urlView = "viewEnterprise?id=param-id";
                        urlView = urlView.replace('param-id', encodeURIComponent(item.enterpriseId))

                        var urlEdit = "editEnterprise?id=param-id";
                        urlEdit = urlEdit.replace('param-id', encodeURIComponent(item.enterpriseId))

                        result += '<tr>';
                        result += '<td class="colSearchEnterpriseView"><a href="' + urlView  + '" id="' + item.enterpriseId + '" >' + (i + 1) + '</a></td>'
                        result += '<td>' + item.ruc + '</td>';
                        result += '<td>' + item.socialReason + '</td>';
                        result += '<td>' + item.agentName + '</td>';
                        result += '<td>' + item.economicGroupName + '</td>';
                        result += '<td>' + item.sectorName + '</td>';
                        result += '<td>' + item.creationDateAux + '</td>';
                        result += '<td class="colSearchEnterpriseEdit"><a id="' + item.enterpriseId + '" href="' + urlEdit + '">Editar</a></td>';
                        result += '</tr>';
                    });

                    $('#tbSearchEnterprise').html(result);
                    TableManaged.init();
  
                }

                repaintSearchEnterprise();
            }

        });//Fin del Ajax

}

function prueba() {

    jsonEconomicGroup = {
        name : "LAIVE",
        credibility : 5
    };

    $.ajax({
        type: "POST",
        data: JSON.stringify(jsonEconomicGroup),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: "insertEconomicGroup",
        success: function (data) {

        }

    });//Fin del Ajax

}
